package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.query.PageResult;
import cn.wolfcode.query.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;

@Service
@Transactional
public class IRoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Override
    public void insert(Role role, Long[] ids) {
        roleMapper.insert(role);

        if(ids!=null&&ids.length>0){
            for(Long id:ids){
                roleMapper.insertRation(role.getId(),id);
            }
        }
    }

    @Override
    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Role role, Long[] ids) {
        roleMapper.updateByPrimaryKey(role);

        roleMapper.deleteRation(role.getId());
        if(ids!=null&&ids.length>0){
            for(Long id:ids){
                roleMapper.insertRation(role.getId(),id);
            }
        }
    }

    @Override
    public Role selectOne(Long id) {
        Role role = roleMapper.selectByPrimaryKey(id);
        return role;
    }

    @Override
    public List<Role> selectAll() {
        List<Role> list = roleMapper.selectAll();
        return list;
    }

    @Override
    public PageInfo<Role> query(QueryObject queryObject) {
        PageHelper.startPage(queryObject.getCurrentPage(), queryObject.getPageSize());
        List<Role> list = roleMapper.queryForList(queryObject);
        return new PageInfo(list);
    }
}
